<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        .traffic-cell {
            font-family: monospace;
        }
        .download { color: #28a745; }
        .upload { color: #007bff; }
        .duration-cell { color: #6c757d; }
        .time-cell { color: #495057; }
        .error {
            color: red;
            background-color: #ffe6e6;
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
        }
        .success {
            color: green;
            background-color: #e6ffe6;
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 10px 5px;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>OpenVPN 数据测试页面</h1>
        
        <div id="status"></div>
        
        <button onclick="loadData()">加载数据</button>
        <button onclick="clearData()">清空数据</button>
        
        <table id="data-table">
            <thead>
                <tr>
                    <th>用户名</th>
                    <th>虚拟地址</th>
                    <th>接收流量</th>
                    <th>发送流量</th>
                    <th>在线时长</th>
                    <th>连接开始时间</th>
                    <th>最后更新</th>
                </tr>
            </thead>
            <tbody id="table-body">
                <tr>
                    <td colspan="7" style="text-align: center; color: #666;">
                        点击"加载数据"按钮获取数据
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        async function loadData() {
            const statusDiv = document.getElementById('status');
            const tbody = document.getElementById('table-body');
            
            statusDiv.innerHTML = '<div class="success">正在加载数据...</div>';
            
            try {
                const response = await fetch('/api/online-users');
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const users = await response.json();
                
                if (users.length === 0) {
                    tbody.innerHTML = '<tr><td colspan="7" style="text-align: center; color: #666;">暂无在线用户</td></tr>';
                    statusDiv.innerHTML = '<div class="success">数据加载完成，暂无在线用户</div>';
                    return;
                }
                
                let html = '';
                users.forEach(user => {
                    const connectedTime = user.connected_since ? new Date(user.connected_since).toLocaleString('zh-CN') : '未知';
                    const lastUpdated = user.last_updated ? new Date(user.last_updated).toLocaleString('zh-CN') : '未知';
                    
                    html += `
                        <tr>
                            <td><strong>${user.username}</strong></td>
                            <td><code>${user.virtual_address}</code></td>
                            <td class="traffic-cell download">${user.traffic_received}</td>
                            <td class="traffic-cell upload">${user.traffic_sent}</td>
                            <td class="duration-cell">${user.duration}</td>
                            <td class="time-cell">${connectedTime}</td>
                            <td class="time-cell">${lastUpdated}</td>
                        </tr>
                    `;
                });
                
                tbody.innerHTML = html;
                statusDiv.innerHTML = `<div class="success">数据加载完成，共 ${users.length} 个用户</div>`;
                
                // 显示原始数据用于调试
                console.log('API返回的原始数据:', users);
                
            } catch (error) {
                console.error('加载数据失败:', error);
                tbody.innerHTML = '<tr><td colspan="7" style="text-align: center; color: red;">加载失败: ' + error.message + '</td></tr>';
                statusDiv.innerHTML = '<div class="error">加载失败: ' + error.message + '</div>';
            }
        }
        
        function clearData() {
            const tbody = document.getElementById('table-body');
            const statusDiv = document.getElementById('status');
            
            tbody.innerHTML = '<tr><td colspan="7" style="text-align: center; color: #666;">数据已清空</td></tr>';
            statusDiv.innerHTML = '<div class="success">数据已清空</div>';
        }
        
        // 页面加载时自动加载数据
        window.addEventListener('load', () => {
            setTimeout(loadData, 1000);
        });
    </script>
</body>
</html> 